package com.jt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jt.dto.UserPageDto;
import com.jt.mapper.MenuMapper;
import com.jt.mapper.RoleMapper;
import com.jt.mapper.UserAndRoleMapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.Menu;
import com.jt.pojo.Permission;
import com.jt.pojo.Role;
import com.jt.pojo.User;
import com.jt.pojo.UserAndRole;
import com.jt.service.UserService;
import com.mysql.cj.util.StringUtils;
import io.jsonwebtoken.lang.Collections;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MenuMapper routerMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserAndRoleMapper userAndRoleMapper;

    @Override
    public List<User> selectAllUserList() {
        return userMapper.selectList(null);
    }


    @Override
    public User selectByNameAndPassWorld(String name, String passWold) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        queryWrapper.eq("passWold", passWold);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean insert(User user) {
        user.setDeleteType("0");
        int count = userMapper.insert(user);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<User> UserPageList(UserPageDto userPageDto) {
        return userMapper.UserPageList(userPageDto);
    }

    @Override
    public int UserTotal(UserPageDto userPageDto) {
        return userMapper.UserTotal(userPageDto);
    }

    @Override
    public boolean AssignPermissions(String StringListRoleName, String userId) {
        List<String> RoleNameList = Arrays.stream(StringListRoleName.split(",")).collect(Collectors.toList());
        List<String> roleIds = roleMapper.AssignPermissions(RoleNameList);
        List<UserAndRole> userAndRoleList = new ArrayList<>();
        roleIds.forEach( oneRoleId-> {
            UserAndRole userAndRole = new UserAndRole();
            userAndRole.setId(UUID.randomUUID().toString());
            userAndRole.setUserId(userId);
            userAndRole.setRoleId(oneRoleId);
            userAndRoleList.add(userAndRole);
        });
        int delete = userAndRoleMapper.delete(new QueryWrapper<UserAndRole>().eq("userId", userId));
        int count = userAndRoleMapper.insertList(userAndRoleList);
        if (count < 0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteById(String userId) {
        int i = userMapper.deleteById(userId);
        return i > 0 ? true : false;
    }

    @Override
    public Map<String, Object> selectMenuList(String userId) {
        Map<String, Object> map = new HashMap<>();
        /*先获取用户的角色*/
        List<String> roleIds = userMapper.getUserRoleList(userId);
        Set<Menu> menuAll = new HashSet<>();
        roleIds.forEach(roleId -> {
            List<Menu> menuList = userMapper.selectMenuList(roleId);
            menuList.forEach(one -> {
                menuAll.add(one);
            });
        });
        /*获取用户所有的权限*/
        List<Menu> permissionAll = menuAll.stream().filter(item -> item.getType().equals("2")).collect(Collectors.toList());
        List<String> permissionList = permissionAll.stream().map(Menu::getPermission).collect(Collectors.toList());
        map.put("permissionList", permissionList);
        /*获取用户菜单信息*/
        List<Menu> menuList = menuAll.stream().filter(item -> !item.getType().equals("2")).collect(Collectors.toList());
        List<Menu> oneMenu = menuList.stream().filter(a -> a.getType().equals("0")).collect(Collectors.toList());
        oneMenu.forEach(one -> {
            getMenuChildren(menuList, one);
        });
        map.put("menuList", oneMenu);
        return map;
    }

    public List<Menu> getMenuChildren(List<Menu> menuList, Menu childrenOne) {
        List<Menu> childrenList = menuList.stream().filter(a -> a.getParentId().equals(childrenOne.getId())).collect(Collectors.toList());
        childrenOne.setChildren(childrenList);
        if (!Collections.isEmpty(childrenList)) {
            childrenList.forEach(children -> {
                getMenuChildren(menuList, children);
            });
        }
        return childrenList;
    }
}
